<!--
 * @Description: 设备类型管理页面
-->
<template>
  <BaseMainSlot :show-tab="true">
    <template v-slot:btn-box>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :show-save="false"
        :show-cancel="false"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:tab-select>
      <SelcetButton @topitemtype="topitemtype" />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">设备类型名称:</div>
          <div class="input">
            <el-input
              v-model="equipmentTypeName"
              placeholder="请输入设备类型名称"
              clearable
            />
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <el-tree
        ref="tree"
        :props="treerule"
        :data="treeData"
        show-checkbox
        node-key="equipmentTypeId"
        default-expand-all
        :check-strictly="true"
        :accordion="true"
        @node-click="handleEdit"
        @check-change="handleSelectionChange"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="dialogTitle + '设施分组信息'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="类型名称:" prop="equipmentTypeName" class="w100p">
            <el-input
              v-model="formData.equipmentTypeName"
              placeholder="请输入设备类型名称"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
          <el-form-item label="备注:" prop="specification" class="w100p">
            <el-input
              v-model="formData.remark"
              type="textarea"
              placeholder="请输入备注(选填)"
              :autosize="{ minRows: 2, maxRows: 4 }"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import equipTypeManaget from "@/api/digital-operation/equip-fac-manage/equip-type-managet/index";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import SelcetButton from "../components/selcetButton/index.vue"
import { mymixin } from "@/utils/mixins";
import SearchBox from "@/components/SearchBox";
export default {
  name: "",
  components: { ActionBar, BaseMainSlot, SelcetButton, SearchBox },
  mixins: [mymixin],
  data() {
    return {
      treerule: {
        children: "child",
        label: "equipmentTypeName"
      },
      treeData: [],
      lessee: "",
      equipmentTypeName: "",
      topitemtypeId: '',
      rules: {
        equipmentTypeName: [
          { required: true, message: "请输入设备类型名称", trigger: "blur" }
        ]
      },
      dialogTitle: "",
      dialogFormVisible: false
    };
  },
  watch: {},
  created() {
    // // 获取树状图
    // this.getTreeDataList();
  },
  mounted() {},
  methods: {
    // 获取点击的分页index
    topitemtype(e) {
      this.topitemtypeId = e;
      // 获取表格数据
      // console.log(e)
      this.formData = {};
      this.treeData = []
      this.equipmentTypeName = ''
      setTimeout(() => {
        this.getTreeDataList()
      }, 100)
    },
    // 获取树状图数据
    getTreeDataList() {
      this.loading = true;
      let params = {
        groupId: this.topitemtypeId
      }
      if (this.equipmentTypeName.length > 0) {
        equipTypeManaget.queryTreeList(this.equipmentTypeName).then(res => {
          this.loading = false;
          let data = res.data;
          // 赋值数组
          if (data) {
            this.treeData = data;
            this.resetActionBar();
          }
        });
      } else {
        equipTypeManaget.getTreeList(params).then(res => {
          this.loading = false;
          let data = res.data;
          // 赋值数组
          if (data) {
            this.treeData = data;
            this.resetActionBar();
          }
        });
      }
    },
    search() {
      this.getTreeDataList();
    },
    add() {
      this.formData = {};
      this.openDialog('add');
    },
    handleEdit(row) {
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      this.canInput = false;
      this.openDialog('edit')
      this.$refs.formData.resetFields();
    },
    handleSelectionChange() {
      let list = this.$refs.tree.getCheckedKeys();
      this.equipmentTypeIdList = [];
      this.equipmentTypeIdList = list;
      // console.log(list);
      if (this.equipmentTypeIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      equipTypeManaget
        .allmetInfo({ equipmentTypeIdList: this.equipmentTypeIdList }, "delList")
        .then(res => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.getTreeDataList();
            this.resetActionBar();
          } else {
            this.$message.warning(res.msg || '删除失败');
          }
        });
      this.$refs.tree.setCheckedKeys([]);
      this.resetActionBar();
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            let nowdataone = {
              remark: this.formData.remark,
              parentId: this.formData.parentId,
              equipmentTypeName: this.formData.equipmentTypeName,
              equipmentTypeId: this.formData.equipmentTypeId,
              groupId: this.topitemtypeId
            };
            equipTypeManaget.allmetInfo(nowdataone, "edit").then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                this.$refs[formData].resetFields()
                this.getTreeDataList();
                this.closeDialog();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            let nowdata = {
              organizationNo: sessionStorage.organizationNo,
              remark: this.formData.remark,
              equipmentTypeName: this.formData.equipmentTypeName,
              groupId: this.topitemtypeId
            };
            if (this.formData.equipmentTypeId) {
              nowdata.parentId = this.formData.equipmentTypeId;
            } else {
              nowdata.parentId = 0;
            }
            // 新增接口
            equipTypeManaget.allmetInfo(nowdata, "add").then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                this.$refs[formData].resetFields()
                this.closeDialog();
                this.getTreeDataList();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          } else {
            this.$message.fail("选择失败！");
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      // eslint-disable-next-line eqeqeq
      this.dialogTitle = type == 'add' ? '新增' : '编辑'
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = '';
    }
  }
};
</script>

<style scoped lang="scss">
.search-box {
  .label-input {
    .label {
      max-width: 140px !important;
    }
  }
}
::v-deep .butt-list-sty {
  position:absolute;
  width: 30% !important;
  transform: translateY(-150%) !important;
  .basebut1 {
  transform: translateY(0%) !important;

  }
  .basebut2 {
    transform: translateY(0%) !important;
  }
}
</style>
